-- UIRotineShopDetail
-- Create by cheny3 Nov/22/2015
-- 碎片兑换商品详细信息界面

-- UIRotineShopDetail继承自Layer
UIRotineShopDetail = class("UIRotineShopDetail", function()
    return cc.Layer:create();
end);

function UIRotineShopDetail.create(goodsIndex)
    return UIRotineShopDetail.new(goodsIndex);
end

-- 构造函数
function UIRotineShopDetail:ctor(goodsIndex)
    self:setName("UIRotineShopDetail");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/shop/BlackMarketGoodsDetail.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录商品信息
    self.goodsIndex = goodsIndex;

    -- 信息
    self.info = RotineActivityM.getUserRotineInfo();
    local bonusInfo = self.info["rotine_bonus"];

    self.goodsInfo = bonusInfo[goodsIndex];

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册事件处理回调函数
function UIRotineShopDetail:registerEventCallback()

end

-- 注册点击事件
function UIRotineShopDetail:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIRotineShopDetail");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确认购买按钮点击事件
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    local function onBuyClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 尝试购买
            self:tryBuy();
        end
    end
    btnBuy:addTouchEventListener(onBuyClick);
    TextStyleM.setTextStyle(btnBuy, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBuy:setTitleText(getLocStr("btn_text_confirm_exchange"));
end

-- 奖励的信息
function UIRotineShopDetail:getBonusInfo(bonus)
    local ret = {};
    local name;
    local desc;
    local detail;
    if bonus[1] == 1 then
        name = ItemM.query(bonus[2], "name");
        desc = ItemM.getItemDesc(bonus[2]);
        detail = ItemM.getItemDetail(bonus[2]);
    elseif bonus[1] == 2 then
        name = FieldsM.query(bonus[2], "name");
        desc = FieldsM.query(bonus[2], "desc");
        detail = FieldsM.query(bonus[2], "detail");
    end

    if type(detail) ~= "string" or #detail <= 0 then
        -- 没有详细描述
        detail = nil;
    elseif bonus[1] == 1 and ItemM.query(bonus[2], "group") == 17 then
        -- 碎片不需要
        detail = nil;
    end

    return {["name"] = name, ["desc"] = desc, ["detail"] = detail};
end

-- 重绘
function UIRotineShopDetail:redraw()
    -- 显示商品名称
    local CT = findChildByName(self.node, "CT");
    local nameLabel = CT:getChildByName("name");

    local bonus = self.goodsInfo["bonus"][1];
    local bonusInfo = self:getBonusInfo(bonus);

    local nameDesc;
    if bonus[3] > 1 then
        nameDesc = string.format(getLocStr("item_bonus_format"), bonusInfo["name"], bonus[3]);
    else
        nameDesc = bonusInfo["name"];
    end
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(nameDesc);

    -- 显示商品描述
    local desc = bonusInfo["desc"];
    local detail = bonusInfo["detail"];

    local descLabel = CT:getChildByName("desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    local detailLabel = CT:getChildByName("detail");
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    if detail then
        detail = "[blue]" .. detail .. "[-]";
        detailLabel = applyString(detailLabel, detail);
        detailLabel:setVisible(true);
    else
        detailLabel:setVisible(false);
    end

    -- 显示商品价格
    local price = RotineActivityM.queryBonus(self.goodsInfo["id"], "price");
    local cost = price[self.info["rotine_type"]];

    local costName = getLocStr("rotine_attrib_rotine_score");
    local num = cost[2];
    local ownAmount = self.info["rotine_score"] or 0;
    local itemDesc = string.format(getLocStr("attrib_value_format"), ownAmount, num);
    if num > ownAmount then
        itemDesc = "[red]" .. itemDesc .. "[-]";
    end
    local costDesc = string.format(getLocStr("change_cost_scraps2"), costName, itemDesc);
    costDesc = costDesc .. "\n";

    -- 显示拥有商品数量
    local amountLabel = CT:getChildByName("own_amount");
    TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GREEN);
    local amount = 0;
    local curTip = getLocStr("own_goods_amount2");
    if bonus[1] == 1 then
        amount = ItemM.getOwnedAmount(ME.user, bonus[2]);
    elseif bonus[1] == 2 then
        amount = ME.user:queryAttrib(bonus[2]);
        curTip = getLocStr("own_goods_amount4");
    end
    costDesc = costDesc .. string.format(curTip, amount);
    amountLabel = applyString(amountLabel, costDesc);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { nameLabel, descLabel, detailLabel, amountLabel, btnBuy});

    -- 隐藏商品阶位
    local starNode = findChildByName(self.node, "CT/star_node");
    starNode:setVisible(false);

    -- 按钮置灰判断
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    if RotineActivityM.canExchangeBonus(self.goodsIndex) ~= true then
        btnBuy:setBright(false);
        btnBuy:setTouchEnabled(false);
    end
end

-- 适配
function UIRotineShopDetail:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 尝试购买
function UIRotineShopDetail:tryBuy()
    -- 判断能否兑换
    local ret = RotineActivityM.canExchangeBonus(self.goodsIndex);
    if ret ~= true then
        AudioM.playFx("market_buy_failed");
        if ret == "sold_out" then
            -- 商品已售完
            alert(getLocStr("goods_sold_out"));
        elseif ret == "lack_item" then
            -- 属性不足
            alert(getLocStr("rotine_score_lack_item"));
        else
            -- 未知原因
            cclog("购买商品失败，原因：" .. ret );
        end
        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIRotineShopDetail");
        return;
    end

    -- 执行购买
    AudioM.playFx("market_buy_success");
    RotineActivityM.exchangeBonus(self.goodsIndex);

    -- 关闭当前界面
    UIMgr.getCurrentScene():removeFormByName("UIRotineShopDetail");
end

